import { OrthographicCamera, sRGBEncoding, WebGLRenderer } from "three";
import LockCameraCtrl from "../ctrl/LockCameraCtrl";


/**
 * @description 2D正交场景
 * @author songmy
 */
export default class HudScene {

     frustumSize = 10;
    /** 摄像机:正交 */
     _camera;
    /** 渲染 */
     _render;
    /** 视口宽 */
     _viewWidth;
    /** 视口高 */
     _viewHeight;
    /** 控制器 */
     _controls;

    get controls() {
        return this._controls;
    }

    get camera() {
        return this._camera;
    }

    get render() {
        return this._render;
    }

    set viewWidth(value) {
        this._viewWidth = value;
    }

    get viewWidth() {
        return this._viewWidth;
    }

    set viewHeight(value) {
        this._viewHeight = value;
    }

    get viewHeight() {
        return this._viewHeight;
    }

    constructor() {

        this._viewWidth = 240;
        this._viewHeight = 208;
        this.createCamera();
        this.createRender();
        this.createControls();
    }

     createCamera() {
        let aspect = this._viewWidth / this._viewHeight;
        this._camera = new OrthographicCamera(this.frustumSize * aspect / - 2, this.frustumSize * aspect / 2, this.frustumSize / 2, this.frustumSize / - 2, 0.1, 1000);
        this._camera.position.set(0, 100, 0);
    }

     createRender() {
        this._render = new WebGLRenderer({
            precision: "highp", // 着色器精度:高
            antialias: true, // 锯齿
            alpha: true, // canvas是否包含alpha (透明度)
            // logarithmicDepthBuffer: true, //是否使用对数深度缓存
            // preserveDrawingBuffer: true //是否保存绘图缓冲
        });
        // 设置尺寸
        this._render.setSize(this._viewWidth, this._viewHeight);
        // 设置设备的物理像素比
        this._render.setPixelRatio(window.devicePixelRatio);
        // 是否渲染阴影
        this._render.outputEncoding = sRGBEncoding;
    }

    /** 添加控制器 */
     createControls() {
        this._controls = new LockCameraCtrl(this._camera, this.render.domElement);
    }

    onResize() {
        // if (BIM.ctrlmode == CurMode.ORTHO) {
        //     this._viewWidth = window.innerWidth;
        //     this._viewHeight = window.innerHeight;
        // }
        // 渲染
        this._render.setSize(this._viewWidth, this._viewHeight);
        // 正交相机
        let aspect = this._viewWidth / this._viewHeight;
        this._camera.left = - this.frustumSize * aspect / 2;
        this._camera.right = this.frustumSize * aspect / 2;
        this._camera.top = this.frustumSize / 2;
        this._camera.bottom = - this.frustumSize / 2;
        this._camera.updateProjectionMatrix();
    }

    onsetview(obj) {

        this._viewWidth = obj.width;
        this._viewHeight = obj.height;
        this.onResize();
    }


    dispose() {

    }
}